<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Admin\Single;
use App\Admin\Category;
use App\Admin\GetCategory;
use App\Admin\Help;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class HelpController extends Controller
{
    //帮帮列表显示/
    public function helpshow(Request $request){
        $help = DB::table('help')->get()->toArray();
//        dd($help);
        return view('admin/help/HelpList',['help'=>$help]);
    }

    //添加帮帮
    public function helpadd(){
        return view('admin/help/HelpAdd');
    }
    //表单的验证添加
    public function helpadddata(Request $request){
//        dd($request);
        //验证规则
        $roles = [
            'why'=>'required|between:1,60',
            'result'=>'required|between:5,250',
        ];
        //自定义的错误信息
        $msg = [
            'why.required' => '问题不能为空',
            'why.between'=>'问题必须在:min和:max之间',
            'result.required' => '解答不能为空',
            'result.between'=>'解答必须在:min和:max之间',
        ];
        $validator = Validator::make($request->all(), $roles, $msg);
        if ($validator->fails()) {
            //验证失败
            return redirect('admin/HelpAdd')->withErrors($validator);
        }

        $input = $request->all();
        $res=Help::create($input);
        return redirect('admin/HelpAdd');
    }
    //修改帮帮
    public function helpedit(){
        $id = $_GET['id'];
        $help = Help::where('id','=',$id)->get();

        return view('admin/help/HelpEdit',['help'=>$help,'id'=>$id]);
    }
    //提交修改表处理
    public function helpeditdata(Request $request){
        //接收要修改的ID
        $id = $request->id;
        //验证规则
        $roles = [
            'why'=>'required|between:1,60',
            'result'=>'required|between:5,250',
        ];
        //自定义的错误信息
        $msg = [
            'why.required' => '问题不能为空',
            'why.between'=>'问题必须在:min和:max之间',
            'result.required' => '解答不能为空',
            'result.between'=>'解答必须在:min和:max之间',
        ];
        $validator = Validator::make($request->all(), $roles, $msg);
//             $this->validate($request, $roles, $msg);
        if ($validator->fails()) {
            //验证失败
            return redirect('admin/HelpEdit?id='.$id)->withErrors($validator);
        }

        $input = Input::except('_token');
        $res =Help::where('id',$id)->update($input);
        return redirect('admin/HelpList');
    }
    //删除帮帮
    public function helpdel(){
        //见分类数据库表遍历到后台
        $id = $_GET['id'];

        $Category = Help::find($id);
        $CateList = $Category->delete();

        return redirect('admin/HelpList');
    }
}
